<template>
  <el-dialog
    :title="!dataForm.recipientId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="idNumber">
      <el-input v-model="dataForm.idNumber" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="learningSection">
      <el-input v-model="dataForm.learningSection" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="school">
      <el-input v-model="dataForm.school" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="accountNumber">
      <el-input v-model="dataForm.accountNumber" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="receivingAddress">
      <el-input v-model="dataForm.receivingAddress" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="targetMoney">
      <el-input v-model="dataForm.targetMoney" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="politicalOutlook">
      <el-input v-model="dataForm.politicalOutlook" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="age">
      <el-input v-model="dataForm.age" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="totalMoney">
      <el-input v-model="dataForm.totalMoney" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recipientId: 0,
          idNumber: '',
          name: '',
          learningSection: '',
          school: '',
          accountNumber: '',
          receivingAddress: '',
          targetMoney: '',
          politicalOutlook: '',
          birthday: '',
          age: '',
          createTime: '',
          totalMoney: '',
          status: ''
        },
        dataRule: {
          idNumber: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          learningSection: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          school: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          accountNumber: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          receivingAddress: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          targetMoney: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          politicalOutlook: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          totalMoney: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recipientId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recipientId) {
            this.$http({
              url: this.$http.adornUrl(`/donation/basicinformationdonation/info/${this.dataForm.recipientId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.idNumber = data.basicInformationDonation.idNumber
                this.dataForm.name = data.basicInformationDonation.name
                this.dataForm.learningSection = data.basicInformationDonation.learningSection
                this.dataForm.school = data.basicInformationDonation.school
                this.dataForm.accountNumber = data.basicInformationDonation.accountNumber
                this.dataForm.receivingAddress = data.basicInformationDonation.receivingAddress
                this.dataForm.targetMoney = data.basicInformationDonation.targetMoney
                this.dataForm.politicalOutlook = data.basicInformationDonation.politicalOutlook
                this.dataForm.birthday = data.basicInformationDonation.birthday
                this.dataForm.age = data.basicInformationDonation.age
                this.dataForm.createTime = data.basicInformationDonation.createTime
                this.dataForm.totalMoney = data.basicInformationDonation.totalMoney
                this.dataForm.status = data.basicInformationDonation.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/donation/basicinformationdonation/${!this.dataForm.recipientId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recipientId': this.dataForm.recipientId || undefined,
                'idNumber': this.dataForm.idNumber,
                'name': this.dataForm.name,
                'learningSection': this.dataForm.learningSection,
                'school': this.dataForm.school,
                'accountNumber': this.dataForm.accountNumber,
                'receivingAddress': this.dataForm.receivingAddress,
                'targetMoney': this.dataForm.targetMoney,
                'politicalOutlook': this.dataForm.politicalOutlook,
                'birthday': this.dataForm.birthday,
                'age': this.dataForm.age,
                'createTime': this.dataForm.createTime,
                'totalMoney': this.dataForm.totalMoney,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
